'use strict';

goog.provide('Blockly.Python.customs');

goog.require('Blockly.Python');


Blockly.Python.customs_digital_pin = function() {
  // Numeric value.
  var code = Blockly.Python.valueToCode(this, 'number_values', Blockly.Python.ORDER_UNARY_SIGN);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Python.ORDER_UNARY_SIGN : Blockly.Python.ORDER_ATOMIC;
  return [code, order];
};


Blockly.Python.customs_analog_pin = function() {
  // Numeric value.
  var code = Blockly.Python.valueToCode(this, 'string_values', Blockly.Python.ORDER_UNARY_SIGN);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Python.ORDER_UNARY_SIGN : Blockly.Python.ORDER_ATOMIC;
  return [code, order];
};


Blockly.Python.customs_show_highlow = function() {
  var dropdown_d_value = Blockly.Python.valueToCode(this, 'd_values', Blockly.Python.ORDER_UNARY_SIGN);
  // TODO: Assemble JavaScript into code variable.
  if(dropdown_d_value=='HIGH'){
    dropdown_d_value=1
  }else{
    dropdown_d_value=0
  }
  var code = dropdown_d_value;
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.Python.ORDER_STRING_CONVERSION];
};



Blockly.Python.customs_digital_write = function() {
  var value_pin = Blockly.Python.valueToCode(this, 'pin', Blockly.Python.ORDER_ATOMIC);
  var value_p_value = Blockly.Python.valueToCode(this, 'p_value', Blockly.Python.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  Blockly.Python.definitions_['setup_output_' + value_pin]='await self.set_pin_mode([' + value_pin + ', 1])';
  var code = 'await self.digital_write([' + value_pin + ', ' + value_p_value + '])\n';
  return code;
};


Blockly.Python.customs_digital_read = function() {
  var value_pin = Blockly.Python.valueToCode(this, 'pin', Blockly.Python.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
 Blockly.Python.definitions_['setup_output_' + value_pin]='await self.set_pin_mode([' + value_pin + ', 0])';
  var code = 'await self.get_tmp_digital_value('+value_pin+')';
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.Python.ORDER_STRING_CONVERSION];
};

Blockly.Python.customs_analog_write = function() {
  var value_pin = Blockly.Python.valueToCode(this, 'pin', Blockly.Python.ORDER_ATOMIC);
  var value_p_value = Blockly.Python.valueToCode(this, 'p_value', Blockly.Python.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  Blockly.Python.definitions_['setup_output_' + value_pin]='await self.set_pin_mode(["' + value_pin+ '", 1])';
  var code = 'await self.analog_write(["' + value_pin + '", ' + value_p_value + '])\n';
  return code;
};


Blockly.Python.customs_analog_read = function() {
  var value_pin = Blockly.Python.valueToCode(this, 'pin', Blockly.Python.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  Blockly.Python.definitions_['setup_output_' + value_pin]='await self.set_pin_mode(["' + value_pin+ '", 2])';
  var code = 'await self.get_tmp_analog_value("'+value_pin+'")';
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.Python.ORDER_STRING_CONVERSION];
};


Blockly.Python.customs_pulse_pin = function() {
  // Numeric value.
  var code = Blockly.Python.valueToCode(this, 'string_values', Blockly.Python.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Python.ORDER_UNARY_SIGN : Blockly.Python.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Python.customs_Roller_switch_pin = function() {
  // Numeric value.
  var code = Blockly.Python.valueToCode(this, 'string_values', Blockly.Python.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Python.ORDER_UNARY_SIGN : Blockly.Python.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Python.customs_Key_switch_pin = function() {
  // Numeric value.
  var code = Blockly.Python.valueToCode(this, 'string_values', Blockly.Python.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Python.ORDER_UNARY_SIGN : Blockly.Python.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Python.customs_Action_triggering_pin = function() {
  // Numeric value.
  var code = Blockly.Python.valueToCode(this, 'string_values', Blockly.Python.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Python.ORDER_UNARY_SIGN : Blockly.Python.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Python.customs_Photosensitive_triggering_pin = function() {
  // Numeric value.
  var code = Blockly.Python.valueToCode(this, 'string_values', Blockly.Python.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Python.ORDER_UNARY_SIGN : Blockly.Python.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Python.customs_Magnetic_switch_pin = function() {
  // Numeric value.
  var code = Blockly.Python.valueToCode(this, 'string_values', Blockly.Python.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Python.ORDER_UNARY_SIGN : Blockly.Python.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Python.customs_Touch_switch_pin = function() {
  // Numeric value.
  var code = Blockly.Python.valueToCode(this, 'string_values', Blockly.Python.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Python.ORDER_UNARY_SIGN : Blockly.Python.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Python.customs_Threshold_trigger_pin = function() {
  // Numeric value.
  var code = Blockly.Python.valueToCode(this, 'string_values', Blockly.Python.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Python.ORDER_UNARY_SIGN : Blockly.Python.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Python.customs_potentiometer_pin = function() {
  // Numeric value.
  var code = Blockly.Python.valueToCode(this, 'string_values', Blockly.Python.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Python.ORDER_UNARY_SIGN : Blockly.Python.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Python.customs_Pressure_sensing_pin = function() {
  // Numeric value.
  var code = Blockly.Python.valueToCode(this, 'string_values', Blockly.Python.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Python.ORDER_UNARY_SIGN : Blockly.Python.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Python.customs_Photosensitive_sensor_pin = function() {
  // Numeric value.
  var code = Blockly.Python.valueToCode(this, 'string_values', Blockly.Python.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Python.ORDER_UNARY_SIGN : Blockly.Python.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Python.customs_Sound_sensor_pin = function() {
  // Numeric value.
  var code = Blockly.Python.valueToCode(this, 'string_values', Blockly.Python.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Python.ORDER_UNARY_SIGN : Blockly.Python.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Python.customs_Grayscale_detection_module_pin = function() {
  // Numeric value.
  var code = Blockly.Python.valueToCode(this, 'string_values', Blockly.Python.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Python.ORDER_UNARY_SIGN : Blockly.Python.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Python.customs_Remote_sensing_module_pin = function() {
  // Numeric value.
  var code = Blockly.Python.valueToCode(this, 'string_values', Blockly.Python.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Python.ORDER_UNARY_SIGN : Blockly.Python.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Python.customs_Ultrasonic_range_finder_pin = function() {
  // Numeric value.
  var code = Blockly.Python.valueToCode(this, 'string_values', Blockly.Python.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Python.ORDER_UNARY_SIGN : Blockly.Python.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Python.customs_Sliding_rheostat_pin = function() {
  // Numeric value.
  var code = Blockly.Python.valueToCode(this, 'string_values', Blockly.Python.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Python.ORDER_UNARY_SIGN : Blockly.Python.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Python.customs_Infrared_detector_pin = function() {
  // Numeric value.
  var code = Blockly.Python.valueToCode(this, 'string_values', Blockly.Python.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Python.ORDER_UNARY_SIGN : Blockly.Python.ORDER_ATOMIC;
  return [code, order];
};


Blockly.Python.customs_motor_m = function() {
  var dropdown_d_value = this.getFieldValue('text_values');
  // TODO: Assemble JavaScript into code variable.
  var code = dropdown_d_value;
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python.customs_motor_direction = function() {
  var dropdown_d_value = this.getFieldValue('text_values');
  // TODO: Assemble JavaScript into code variable.
  var code = dropdown_d_value;
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python.customs_servo_motor_single = function() {
    //正转 D5>D3 D9>D6
    //反转D3>D5  D6>D9
  var dropdown_motor_m = Blockly.Python.valueToCode(this, 'MOTOR_M', Blockly.Python.ORDER_ATOMIC);
  var dropdown_direction_set = Blockly.Python.valueToCode(this, 'DIRECTION_SET', Blockly.Python.ORDER_ATOMIC);
  var number_speed_set_val = Blockly.Python.valueToCode(this, 'SPEED_SET_VAL', Blockly.Python.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
    var PIN_list=[];
    var min_speed=0;  //其中最小的输出
    if(dropdown_motor_m=='M1'){// 第一个电机
        if(dropdown_direction_set=='F'){  //正转
            PIN_list[0]='5';
            PIN_list[1]='3';
        }
        else{
            PIN_list[0]='3';
            PIN_list[1]='5';
        }
    }
    else{
        if(dropdown_direction_set=='F') {  //正转
            PIN_list[0] = '6';
            PIN_list[1] = '9';
        }
        else{
            PIN_list[0] = '9';
            PIN_list[1] = '6';
        }
    }

     Blockly.Python.definitions_['setup_output_' + PIN_list[0]]='await self.set_pin_mode(["' + PIN_list[0]+ '", 1])';
     Blockly.Python.definitions_['setup_output_' + PIN_list[1]]='await self.set_pin_mode(["' + PIN_list[1]+ '", 1])';
    var code = 'await self.analog_write(["' + PIN_list[0] + '", ' + number_speed_set_val + '])\n';
    code=code+'await self.analog_write(["' + PIN_list[1] + '", ' + min_speed + '])\n';
  return code;
};


Blockly.Python.customs_two_motor_direction = function() {
  var dropdown_d_value = this.getFieldValue('text_values');
  // TODO: Assemble JavaScript into code variable.
  var code = dropdown_d_value;
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python.customs_two_motor_speed = function() {
  var dropdown_d_value = this.getFieldValue('text_values');
  // TODO: Assemble JavaScript into code variable.
  var code = dropdown_d_value;
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python.customs_servo_motor_two = function() {
    //正转 D5>D3 D9>D6
    //反转D3>D5  D6>D9
    //D5 D3电机在左边
    //D9 D6电机在右边
  var dropdown_motor_m = Blockly.Python.valueToCode(this, 'MOTOR_M', Blockly.Python.ORDER_ATOMIC);
  var dropdown_speed_set = Blockly.Python.valueToCode(this, 'SPEED_SET', Blockly.Python.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
    var pin_list=['5','3','9','6'];
    var pin_value=[];
    var init_pin_value='0';
    switch(dropdown_motor_m){
        case 'F':
            pin_value=[dropdown_speed_set,init_pin_value,dropdown_speed_set,init_pin_value];
            break;
        case 'B':
            pin_value=[init_pin_value,dropdown_speed_set,init_pin_value,dropdown_speed_set];
            break;
        case 'L':
            var min_set=0;
            pin_value=[dropdown_speed_set,init_pin_value,min_set,init_pin_value];
            break;
        case 'R':
            var min_set=0;
            pin_value=[min_set,init_pin_value,dropdown_speed_set,init_pin_value];
            break;
    }
    var code='';
    for(var i=0;i<pin_list.length;i++){
        Blockly.Python.definitions_['setup_output_' + pin_list[i]]='await self.set_pin_mode(["' + pin_list[i]+ '", 1])';
        code=code+'await self.analog_write(["' + pin_list[i] + '", ' + pin_value[i] + '])\n';
    }
  return code;
};



Blockly.Python.customs_oled_single = function() {
  var value_display_text = Blockly.Python.valueToCode(this, 'DISPLAY_TEXT', Blockly.Python.ORDER_ATOMIC);
  var value_row_x = Blockly.Python.valueToCode(this, 'ROW_X', Blockly.Python.ORDER_ATOMIC);
  var value_column_y = Blockly.Python.valueToCode(this, 'COLUMN_Y', Blockly.Python.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  if (value_display_text==""){
    value_display_text='""'
  }
    var code='await self.show_single_led(['+value_display_text+','+value_row_x+','+value_column_y+'])\n';
  return code;
};


Blockly.Python.customs_oled_display = function() {
  var value_text_one = Blockly.Python.valueToCode(this, 'TEXT_ONE', Blockly.Python.ORDER_ATOMIC);
  var value_text_two = Blockly.Python.valueToCode(this, 'TEXT_TWO', Blockly.Python.ORDER_ATOMIC);
  var value_text_three = Blockly.Python.valueToCode(this, 'TEXT_THREE', Blockly.Python.ORDER_ATOMIC);
  var value_text_four = Blockly.Python.valueToCode(this, 'TEXT_FOUR', Blockly.Python.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  if (value_text_one==""){
    value_text_one='""'
  }
  if(value_text_two==""){
    value_text_two='""'
  }
  if(value_text_three==""){
    value_text_three='""'
  }
  if(value_text_four==""){
    value_text_four='""'
  }
  var code='await self.show_four_led_block(['+value_text_one+','+value_text_two+','+value_text_three+','+value_text_four+'])\n';
  return code;
};


Blockly.Python.customs_tone_rhythm = function() {
  var dropdown_rhythm = this.getFieldValue('number_values');
  // TODO: Assemble JavaScript into code variable.
  var code = dropdown_rhythm;
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python.customs_play_sound = function() {
  var value_tone_play = Blockly.Python.valueToCode(this, 'tone_play', Blockly.Python.ORDER_ATOMIC);
  var value_frequency = Blockly.Python.valueToCode(this, 'frequency', Blockly.Python.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  var code = '';
    code=code+'await self.play_tone(['+value_tone_play+',"TONE_TONE",'+value_frequency+',None])\n';
  return code;
};


Blockly.Python.customs_play_sound_duration = function() {
  var value_tone_play = Blockly.Python.valueToCode(this, 'tone_play', Blockly.Python.ORDER_ATOMIC);
  var value_frequency = Blockly.Python.valueToCode(this, 'frequency', Blockly.Python.ORDER_ATOMIC);
  var value_otime = Blockly.Python.valueToCode(this, 'otime', Blockly.Python.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
    var code='';
    code=code+'await self.play_tone(['+value_tone_play+',"TONE_TONE",'+value_frequency+','+value_otime+'*1000])\n';
  return code;
};



Blockly.Python.customs_stop_sound = function() {
  var value_pin = Blockly.Python.valueToCode(this, 'PIN', Blockly.Python.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  var code = '';
    code=code+'await self.play_tone(['+value_pin+',"TONE_NO_TONE",0,None])\n';
  return code;
};


Blockly.Python.customs_temperature_read = function() {
  var value_pin = Blockly.Python.valueToCode(this, 'pin', Blockly.Python.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  var code = 'temperature_read(' + value_pin + ')';
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.Python.ORDER_STRING_CONVERSION];
};


Blockly.Python.customs_humidity_read = function() {
  var value_pin = Blockly.Python.valueToCode(this, 'pin', Blockly.Python.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  var code = 'humidity_read(' + value_pin + ')';
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.Python.ORDER_STRING_CONVERSION];
};
